package com.winHandle.load;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import com.common.FrameUtil;
import com.common.MessageShow;
import com.common.TimeChange;
import com.service.impl.ProjectServiceImpl;
import com.service.impl.TableServiceImpl;

public class MainWin_T extends BaseFrame{

	private static final long serialVersionUID = 1409797556587284816L;

	private JComboBox search_P ;
	private JTextField search_T ;
	private JButton searchBtn;
	private JButton addBtn;

	private JButton delBtn ;
	private JButton updateBtn ;

	private JTable jTable ;
	private DefaultTableModel model;

	private JLabel time = new JLabel(new SimpleDateFormat("yyyy-MM-dd    hh：mm：ss").format(new Date()));
	private TimeChange timeChange = new TimeChange(time);

	private JPanel top ;
	private JPanel center ;

	String[] projectNames ;

	public void excute(){

		projectNames = new ProjectServiceImpl().getProjectNames();

		/***  布局内容   */
		top = new JPanel();
		search_P = new JComboBox(projectNames);
		search_T = new JTextField(20);
		searchBtn = new JButton("  查 询  ");
		addBtn = new JButton("  添 加  ");

		search_T.setBounds(30, 60, 67, 18);;

		searchBtn.addActionListener(this);
		addBtn.addActionListener(this);

		top.add(search_P);
		top.add(new JLabel("  "));
		top.add(search_T);
		top.add(new JLabel("  "));
		top.add(searchBtn);
		top.add(new JLabel("  "));
		top.add(addBtn);

		//top.setLayout(new FlowLayout(FlowLayout.LEFT));
		jframe.getContentPane().add(top,BorderLayout.NORTH);

		center = new JPanel();

		jTable = new JTable();

		String[] titleArr ={"ID","排序","表名","表单描述"};

		model = new DefaultTableModel();
		model.setColumnIdentifiers(titleArr);
		for(int i = 0 ; i < 15 ; i++){
			model.insertRow(i, new Object[]{});
		}

		jTable.setModel(model);

		JScrollPane scrollpane = new JScrollPane(jTable);
		scrollpane.setPreferredSize(new Dimension(680,350));
		center.add(scrollpane);

		delBtn = FrameUtil.getJButton("  删 除  ", 150, 400, 120, 30);
		updateBtn = FrameUtil.getJButton("  修 改  ", 450, 400, 120, 30);

		jframe.add(delBtn);
		jframe.add(updateBtn);
		delBtn.addActionListener(this);
		updateBtn.addActionListener(this);

		jframe.getContentPane().add(center,BorderLayout.CENTER);

		jframe.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {

		super.actionPerformed(e);
		String searchP_v = search_P.getSelectedItem().toString();
		String searchTXT = search_T.getText();

		if(null == searchP_v || "".equals(searchP_v)){
			MessageShow.warn("请先添加项目！");
			return ;
		}

		if(e.getSource() == searchBtn){

			/*if(false && "".equals(searchTXT)){
				MessageShow.warn("请输入要查询的表单名！");
				return ;
			}else{*/
			// 获取数据 更新model

			new TableServiceImpl().loadModelData(model, searchP_v  ,searchTXT);

			//}
		}else if (e.getSource() == addBtn) {
			new TableWin("添加表单",model, searchP_v , searchTXT);
		}else if (e.getSource() == delBtn) {
			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要删除的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能删除一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();

				if(0 != MessageShow.confirm("确认要删除ID为 [ " + id + " ] 的记录吗？")){
					return ;
				}
				try {
					if(new TableServiceImpl().del(Long.parseLong(id))){
						MessageShow.error("删除成功！");
						new TableServiceImpl().loadModelData(model, searchP_v,searchTXT);
					}else {
						MessageShow.error("删除失败！");
					}
				} catch (NumberFormatException e1) {

					e1.printStackTrace();
					MessageShow.error("参数异常！");
				}

			}
		}else if (e.getSource() == updateBtn) {
			int rowCount = jTable.getSelectedRowCount() ;
			if(rowCount < 1){
				MessageShow.warn("请选择要修改的记录！");
			}else if (rowCount > 1) {
				MessageShow.warn("每次只能操作一条记录！");
			}else{
				Object selectedCell = model.getValueAt(jTable.getSelectedRow(), 0);
				if(null == selectedCell){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				String id = selectedCell.toString();
				if("".equals(id.trim())){
					MessageShow.warn("所选记录无效！");
					return ;
				}
				new TableWin("表单修改", model,searchP_v, Long.parseLong(id), searchTXT, true);
			}
		}
	}

	public MainWin_T() {
		super();
		showFrameName = "TABLE";
		excute();
	}
	public MainWin_T(String title){
		super(title + " -> 表单");
		showFrameName = "TABLE";
		excute();
	}

}
